﻿
package com.wit.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wit.service.WageService;
import com.wit.po.Employee;
import com.wit.po.Employeeaddition;
import com.wit.po.Policy;
import com.wit.po.Wage;

public class WageAction extends ActionSupport{

	private static final String VIEW_WAGE_LIST = "view_wage_list";
	private static final String VIEW_WAGE_INFO = "view_wage_info";
	private static final String START_ADD = "start_add";
	private static final String START_EDIT = "start_edit";
	private static final String VIEW_POLICY_LIST = "view_policy_list";
	
	private String wageState;
	private Integer wageId;
	private Wage wage;
	private String wageCard;
	private Page page;
	
	private Date inMonth;
	private Integer []chks;
	
	private String employeeId;
	private Integer category;
	private Integer year;
	private Integer month;
	private Integer season;
	
	private int totalAmount;
	private Double totalSum;
	
	private WageService wageService;
	
	/*
	 *显示工资列表
	 */
	public String showWageList()
	{
		List wageList = wageService.getWageList(wage, page.getPageNumber(), page.getPageSize());
		
		totalAmount = wageService.getTotalAmount(wage);
		page.setTotalPage(totalAmount % page.getPageSize() == 0 ? totalAmount / page.getPageSize() : totalAmount / page.getPageSize() + 1);
		
		ServletActionContext.getRequest().setAttribute("wageList", wageList);
		return VIEW_WAGE_LIST;
	}
	public void validateShowWageList()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
		else if(page == null)
			page = new Page();
		else
		{
			page.check();
		}
		if("true".equals(wageState))
		{
			wage.setWageState(true);
		}
		else if("false".equals(wageState))
		{
			wage.setWageState(false);
		}
	}
	
	private boolean checkIdentity()
	{
		if((Employee)ActionContext.getContext().getSession().get("employee") == null)
			return false;
		return true;
	}
	
	private Employee getCurrentEmployee()
	{
		return (Employee)ActionContext.getContext().getSession().get("employee");
	}
	
	/*
	 *显示工资详细信息
	 */
	public String showWageInfo()
	{
		wage = wageService.getWageById(wageId);
		return VIEW_WAGE_INFO;
	}
	public void validateShowWageInfo()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
		else if(wageId == null)
			addActionError("未指定要查看的工资");
	}
	
	public String startEdit()
	{
		wage = wageService.getWageById(wageId);
		return START_EDIT;
	}
	public void validateStartEdit()
	{
		
	}
	
	/*
	 *更新工资信息(如：结算工资)
	 */
	public String updateWage()
	{
		if(wageService.updateWage(wage))
			return VIEW_WAGE_INFO;
		return START_EDIT;
	}
	public void validateUpdateWage()
	{
		Wage t_wage = null;
		if(!checkIdentity())
			addActionError("未登录用户");
		else if ((t_wage = wageService.getWageById(wage.getWageId())) == null)				//   ?????????????????
		{
			addActionError("未指明更新的客户ID");
		}
		else
		{
			wage.setEmployee(t_wage.getEmployee());
			wage.setEmployeeaddition(t_wage.getEmployeeaddition());
			/* wage.setWageState(t_wage.getWageState()); */
			wage.setStandardDate(t_wage.getStandardDate());
			wage.check();
		}
		
	}
	
	/*
	 *  添加工资信息
	 */
	public String addWage()
	{
		String employeeId;
		if(wage == null || wage.getEmployee() == null || (employeeId = wage.getEmployee().getEmployeeId()) == null)
			return START_ADD;
		
		wage.check();
		wage.setEmployeeaddition(new Employeeaddition(employeeId));
		
		if(wageService.addWage(wage))
			return VIEW_WAGE_INFO;
		return START_ADD;
	}
	public void validateAddWage()
	{
		if(!checkIdentity())
		{
			addActionError("未登录用户");
		}
	}
	
	public String addBatchOfWage()
	{
		wageService.generateWage(inMonth);
		return SUCCESS;
	}
	public void validateAddBatchOfWage()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
	}
	
	public String updateBatchOfWage()
	{
		wageService.updateBatchOfWage(chks);
		return SUCCESS;
	}
	public void validateUpdateBatchOfWage()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
	}
	
	public String updateAllWage()
	{
		wage = new Wage();
		wage.setWageState(false);
		List<Wage> wageList = wageService.getWageList(wage,0,-100);
		wageService.updateBatchOfWage(wageList);
		return SUCCESS;
	}
	public void validateUpdateAllWage()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
	}
	
	/*
	 *  删除工资信息
	 */
	public String removeWage()
	{
		wage = wageService.getWageById(wageId);
		if(wage != null)
			wageService.removeWage(wage);
		
		return SUCCESS;
	}
	public void validateRemoveWage()
	{
		if(!checkIdentity())
			addActionError("未登录用户");
		else if(wageId == null)
			addActionError("未指明要删除的工资");
	}
	
	public String showPolicyList()
	{
		List<Policy> policyList = null;
		Object[] o = null;
		switch (category)
		{
		case 0:
			policyList = wageService.getPolicyList(employeeId, year, page.getPageNumber(), page.getPageSize());
			
			o = wageService.getPolicyFee(employeeId, year);
			totalSum = (Double)o[0];
			totalAmount = ((Long)o[1]).intValue();
			
			page.setTotalPage(totalAmount % page.getPageSize() == 0 ? totalAmount / page.getPageSize() : totalAmount / page.getPageSize() + 1);
			
			break;
		case 1:
			policyList = wageService.getPolicyList(employeeId, year, month, page.getPageNumber(), page.getPageSize());
			
			o = wageService.getPolicyFee(employeeId, year, month);
			totalSum = (Double)o[0];
			totalAmount = ((Long)o[1]).intValue();
			
			page.setTotalPage(totalAmount % page.getPageSize() == 0 ? totalAmount / page.getPageSize() : totalAmount / page.getPageSize() + 1);
			
			break;
		case 2:
			policyList = wageService.getPolicyList(employeeId, year, season * 3 - 3, (season * 3), page.getPageNumber(), page.getPageSize());
			
			o = wageService.getPolicyFee(employeeId, year, season * 3 - 3, (season * 3));
			totalSum = (Double)o[0];
			totalAmount = ((Long)o[1]).intValue();
			
			page.setTotalPage(totalAmount % page.getPageSize() == 0 ? totalAmount / page.getPageSize() : totalAmount / page.getPageSize() + 1);
			
			break;
		default:
			category = 0;
			totalAmount = 0;
		}
		ServletActionContext.getRequest().setAttribute("policyList", policyList);
		return VIEW_POLICY_LIST;
	}
	public void validateShowPolicyList()
	{
		if(page == null)
			page = new Page();
		else
		{
			page.check();
		}
	}
	
	/***************************** Getters and Setters *****************************/
	
	public Integer getWageId() {
		return wageId;
	}
	public void setWageId(Integer wageId) {
		this.wageId = wageId;
	}
	public Wage getWage() {
		return wage;
	}
	public void setWage(Wage wage) {
		this.wage = wage;
	}
	public String getWageCard() {
		return wageCard;
	}
	public void setWageCard(String wageCard) {
		this.wageCard = wageCard;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public WageService getWageService() {
		return wageService;
	}
	public void setWageService(WageService wageService) {
		this.wageService = wageService;
	}
	public String getWageState() {
		return wageState;
	}
	public void setWageState(String wageState) {
		this.wageState = wageState;
	}
	public Date getInMonth() {
		return inMonth;
	}
	public void setInMonth(Date inMonth) {
		this.inMonth = inMonth;
	}
	public Integer[] getChks() {
		return chks;
	}
	public void setChks(Integer[] chks) {
		this.chks = chks;
	}
	public String getEmployeeId() {
		return employeeId;
	}
	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}
	public Integer getCategory() {
		return category;
	}
	public void setCategory(Integer category) {
		this.category = category;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
	public Integer getMonth() {
		return month;
	}
	public void setMonth(Integer month) {
		this.month = month;
	}
	public Integer getSeason() {
		return season;
	}
	public void setSeason(Integer season) {
		this.season = season;
	}
	public Double getTotalSum() {
		return totalSum;
	}
	public void setTotalSum(Double totalSum) {
		this.totalSum = totalSum;
	}
	public int getTotalAmount() {
		return totalAmount;
	}
	public void setTotalAmount(int totalAmount) {
		this.totalAmount = totalAmount;
	}
	
}